package com.cloudsigns.factorymanager2.project.activity;

import android.app.Activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.project.adapter.ViewPageAdapter;
import com.cloudsigns.factorymanager2.project.customview.UnderlinePageIndicator;
import com.cloudsigns.factorymanager2.project.fragment.EngineeringInstallationOrderDistributedFragment;
import com.cloudsigns.factorymanager2.project.fragment.EngineeringInstallationOrderEditFragment;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;

/**
 * 工程安装订单 详情页
 * Created by tony on 2017/12/14.
 */

public class EngineeringInstallationOrderDetailActivity extends FragmentActivity implements View.OnClickListener {
    private View rl_back;
    private TextView tv_title;
    private TextView tv_edit_order, tv_distributed_order;
    private ViewPager viewpager;
    private ViewPageAdapter viewpagerAdapter;
    private ArrayList<Fragment> listFragments;
    private EngineeringInstallationOrderEditFragment engineeringInstallationOrderEditFragment;
    private EngineeringInstallationOrderDistributedFragment engineeringInstallationOrderDistributedFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_engineering_installation_order_detail);

        init();
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        viewpager = findViewById(R.id.viewpager);
        tv_edit_order = findViewById(R.id.tv_edit_order);
        tv_distributed_order = findViewById(R.id.tv_distributed_order);


        tv_title.setText(getResources().getText(R.string.engineering_installation_order));
        rl_back.setOnClickListener(this);
        tv_edit_order.setOnClickListener(this);
        tv_distributed_order.setOnClickListener(this);

        listFragments = new ArrayList<Fragment>();
        if (engineeringInstallationOrderEditFragment == null) {
            engineeringInstallationOrderEditFragment = new EngineeringInstallationOrderEditFragment();
            listFragments.add(engineeringInstallationOrderEditFragment);
        }

        if (engineeringInstallationOrderDistributedFragment == null) {
            engineeringInstallationOrderDistributedFragment = new EngineeringInstallationOrderDistributedFragment();
            listFragments.add(engineeringInstallationOrderDistributedFragment);
        }


        viewpagerAdapter = new ViewPageAdapter(getSupportFragmentManager(), listFragments);
        viewpager.setAdapter(viewpagerAdapter);
        viewpager.setOffscreenPageLimit(2);


//        UnderlinePageIndicator indicator = (UnderlinePageIndicator) findViewById(R.id.indicator);
//        indicator.setViewPager(viewpager);
//        indicator.setFades(false);
//        indicator.setSelectedColor(getResources().getColor(R.color.rgb_17a5ef));
//        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//
//            @Override
//            public void onPageSelected(int position) {
//                showSelectedOption(position);
//            }
//
//            @Override
//            public void onPageScrolled(int arg0, float arg1, int arg2) {
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int arg0) {
//
//            }
//        });

    }


    /**
     * 切换到哪个tab页
     *
     * @param pos
     */
    private void showSelectedOption(int pos) {
        // TODO Auto-generated method stub
        switch (pos) {
            case ConstantUtil.TAB_FRAGMENT_FIRST: //编辑
                tv_edit_order.setBackgroundResource(R.mipmap.tv_bg_top_blue);
                tv_edit_order.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
                tv_distributed_order.setBackgroundResource(R.mipmap.tv_bg_top_white);
                tv_distributed_order.setTextColor(getResources().getColor(R.color.rgb_5B9BD5));
                break;
            case ConstantUtil.TAB_FRAGMENT_SECOND: //派发
                tv_distributed_order.setBackgroundResource(R.mipmap.tv_bg_top_blue);
                tv_distributed_order.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
                tv_edit_order.setBackgroundResource(R.mipmap.tv_bg_top_white);
                tv_edit_order.setTextColor(getResources().getColor(R.color.rgb_5B9BD5));

                break;
        }
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.tv_edit_order: //编辑
                viewpager.setCurrentItem(0);
                showSelectedOption(ConstantUtil.TAB_FRAGMENT_FIRST);
                break;
            case R.id.tv_distributed_order: //派发
                viewpager.setCurrentItem(1);
                showSelectedOption(ConstantUtil.TAB_FRAGMENT_SECOND);
                break;

        }
    }
}
